package cn.yunhe.service.impl;

import cn.yunhe.dao.TopicMapper;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.Topic;
import cn.yunhe.service.ITopicService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TopicService implements ITopicService {

    @Autowired
    @Getter
    @Setter
    private TopicMapper topicMapper;

    @Override
    public Page queryLikeTopic(Map<String, Object> cond) {
        Page page = new Page();
        //根据前台条件查询出记录的条数
        page.setTotalNum(topicMapper.getLikeTopicCount(cond));
        //从前台数据中获得每页的大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        //从前台数据中获得当前页码
        int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurPageNum(curPageNum);
        //动态计算出总页数
        page.setTotalPages(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
        //根据条件获得结果列表
        page.setResult(topicMapper.getLikeTopic(cond));
        return page;
    }

    @Override
    public void add(Topic topic) {
        topicMapper.add(topic);
    }

    @Override
    public void del(Topic topic) {
        topicMapper.del(topic);
    }

    @Override
    public void update(Topic topic) {
        topicMapper.update(topic);
    }

    @Override
    public Topic getTopic(Topic topic) {
        return topicMapper.getTopic(topic);
    }
}
